package com.scott.api.curator;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.api.transaction.CuratorTransactionResult;

import java.util.Collection;

/**
 * @author Scott 2017/11/1 15:51
 */
public class CuratorCreateSessionDemo {

    public static void main(String[] args) throws InterruptedException {
        //创建会话
        CuratorFramework curatorFramework = CuratorClientUtils.getInstance();

        System.out.println("创建连接成功");

        //创建节点
//        try {
//            String path = curatorFramework.create().creatingParentContainersIfNeeded().withMode(CreateMode.PERSISTENT)
//                    .forPath("/curator/curator1/curator11", "123".getBytes());
//            System.out.println("创建节点成功" + path);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

        //删除节点
//        try {
//            //默认情况下，节点的version 为-1
//            curatorFramework.delete().deletingChildrenIfNeeded().forPath("/curator");
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

        //查询数据
//        try {
//            Stat stat = new Stat();
//            byte[] date = curatorFramework.getData().storingStatIn(stat).forPath("/curator/curator1/curator11");
//            System.out.println(new String(date) + "  stat->" + stat);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }


        //更新节点
//        try {
//            Stat stat = curatorFramework.setData().forPath("/curator/curator1", "234".getBytes());
//            System.out.println(stat);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

        //异步操作
        // curator中这些的所有操作都是异步操作，curator进行了友好的封装
//        System.out.println("当前的主线程为：" + Thread.currentThread().getName());
//        CountDownLatch countDownLatch = new CountDownLatch(1);
//        ExecutorService service = Executors.newFixedThreadPool(1);
//        try {
//
//            curatorFramework.create()
//                    .creatingParentContainersIfNeeded()
//                    .withMode(CreateMode.EPHEMERAL)
//                    .inBackground(new BackgroundCallback() {
//                        @Override
//                        public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
//                            System.out.println(Thread.currentThread().getName() + "-> resultCode :" + event.getResultCode() + "->" + event.getType());
//                            countDownLatch.countDown();
//                        }
//                    }, service).forPath("/scott", "lee".getBytes());
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        countDownLatch.await();
//        service.shutdown();


        //事务操作（curator独有的）
        //下面的事例的意思是先创建一个节点，然后在修改一个不存在的节点
        try {
            Collection<CuratorTransactionResult> resultCollection = curatorFramework.inTransaction()
                    .create().forPath("/trans", "scott.Lee".getBytes())
                    .and()
                    .setData().forPath("/XXX", "111".getBytes())
                    .and().commit();

            for (CuratorTransactionResult result:resultCollection){
                System.out.println(result.getForPath() + "->" + result.getType());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
